package com.example.controller.customer;

import com.example.constant.StatusConstant;
import com.example.entity.Inventory;
import com.example.entity.Products;
import com.example.result.Result;
import com.example.service.InventoryService;
import com.example.service.ProductsService;
import com.example.variable.ProductAndStoreId;
import com.example.vo.InventoryVO;
import com.example.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 1. @ClassDescription:
 * 2. @author: Z
 * 3. @date: 2024年03月21日 23:04
 */
@RestController("customerProductsController")
@RequestMapping("/customer/products")
@Slf4j
@Api(tags = "C端-产品接口")
public class ProductsController {

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id和商店id查询商品")
    public Result<List<InventoryVO>> list(Integer categoryId, Integer storeId) {

        //构造redis中的key，dish_id
        String key = "product_" + categoryId + "/store_" + storeId;
        //查询redis中是否存在菜品数据
        List<InventoryVO> list = (List<InventoryVO>) redisTemplate.opsForValue().get(key);
        if(list != null&& list.size()>0){
            //如果存在，直接返回，无须查询数据库
            return Result.success(list);
        }

        ProductAndStoreId products = new ProductAndStoreId();
        products.setCategoryId(categoryId);
        products.setStatus(StatusConstant.ENABLE);//查询起售中的菜品
        products.setStoreId(storeId);
        //如果不存在，查询数据库，并存入redis中
        list = inventoryService.listWithCategoryId(products);
//        list = productsService.listWithFlavor(products);
        redisTemplate.opsForValue().set(key,list);

        return Result.success(list);
    }

}
